package com.xtaller.sdboot.config.ex;

import com.xtaller.sdboot.core.R;
import com.xtaller.sdboot.core.Status;
import lombok.var;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;

/**
 * @author : Taller
 * @date : 2020/3/2 08:54
 * @Description :
 */
// @ControllerAdvice
@ResponseBody
public class ControllerExceptionHandler {

    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public ResponseEntity handleMissingServletRequestParameterException(MissingServletRequestParameterException e, HttpServletRequest request) {
       //  log(request, e);
        return R.error(Status.BAD_REQUEST,"缺少必要的请求参数");
    }

    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseEntity handleHttpMessageNotReadableException(
            HttpMessageNotReadableException e,
            HttpServletRequest request) {
       //  log(request, e);
        return R.error(Status.BAD_REQUEST,"参数解析失败");
    }

    /**
     * 400 - Bad Request 通过
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) {
        FieldError fieldError = e.getBindingResult().getFieldError();
        var message = "";
        if(fieldError != null){
            // fieldError.getField() 字段 => JSR303校验
            message = fieldError.getDefaultMessage();
        }
        return R.error(Status.BAD_REQUEST, message);
    }


    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(BindException.class)
    public ResponseEntity handleBindException(BindException e, HttpServletRequest request) {
       //  log(request, e);
        return R.error(Status.BAD_REQUEST,"参数绑定失败");
    }

    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(ConstraintViolationException.class)
    public ResponseEntity handleServiceException(ConstraintViolationException e, HttpServletRequest request) {
       //  log(request, e);
        return R.error(Status.BAD_REQUEST,"约束验证失败");
    }


    /**
     * 405 - Method Not Allowed
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseEntity handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e,
                                                                       HttpServletRequest request) {
       //  log(request, e);
        return R.error(Status.METHOD_NOT_ALLOWED,String.format("该API不支持[%s]请求方法",e.getMethod()));
    }

    /**
     * 415 - Unsupported Media Type
     */
    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public ResponseEntity handleHttpMediaTypeNotSupportedException(Exception e, HttpServletRequest request) {
       //  log(request, e);
        return R.error(Status.UNSUPPORTED_MEDIA_TYPE,"不支持当前请求数据类型");
    }

    /**
     * 500 - Internal Server Error 自定义异常
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(ServiceException.class)
    public ResponseEntity handleServiceException(ServiceException e, HttpServletRequest request) {
       //  log(request, e);
        return R.error(Status.INTERNAL_SERVER_ERROR,"业务服务异常");
    }

    /**
     * 500 - Internal Server Error
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public ResponseEntity handleException(Exception e, HttpServletRequest request) {
       //  log(request, e);
        return R.error(Status.INTERNAL_SERVER_ERROR,"服务器出现异常");
    }

    /**
     * 操作数据库出现异常:名称重复，外键关联
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(DataIntegrityViolationException.class)
    public ResponseEntity handleException(DataIntegrityViolationException e,
                                          HttpServletRequest request) {
       //  log(request, e);
        return R.error(Status.BAD_REQUEST,"操作数据库出现异常,数据回滚初始状态.");
    }

}
